package com.csxy.lsh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.lsh.api.ServiceEduClient;
import com.csxy.lsh.api.ServiceUcenterClient;
import com.csxy.lsh.entity.TOrder;
import com.csxy.lsh.entity.vo.EduCourseVo;
import com.csxy.lsh.entity.vo.UcenterMemberVo;
import com.csxy.lsh.mapper.OrderMapper;
import com.csxy.lsh.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TOrder> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ServiceEduClient eduClient;
    @Autowired
    private ServiceUcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String memberId) {

        UcenterMemberVo member=ucenterClient.getMemberInfoById(memberId);

        EduCourseVo courseInfo=eduClient.getCourseInfoByIdOrder(courseId);

        TOrder tOrder =new TOrder();
        tOrder.setCourseId(courseId);
        tOrder.setCourseTitle(courseInfo.getTitle());
        tOrder.setCourseCover(courseInfo.getCover());
        tOrder.setTeacherName(courseInfo.getTeacherName());
        tOrder.setMemberId(memberId);
        tOrder.setNickname(member.getNickname());
        tOrder.setMobile(member.getMobile());
        tOrder.setTotalFee(courseInfo.getPrice());
        tOrder.setStatus(0);
        tOrder.setPayType(1);
        tOrder.setOrderNo("ORD-"+ UUID.randomUUID().toString().replace("-", "").substring(0, 8));

        baseMapper.insert(tOrder);

        return tOrder.getOrderNo();







    }
    @Override
    public TOrder getOrderByTradeNo(String tradeNo) {
        // 示例：根据订单号查询
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", tradeNo); // 假设字段名为order_no
        return orderMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateOrder(TOrder order) {
        orderMapper.updateById(order);
    }
}
